﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Legend.Component.Common;
using Legend.Component.Interface;
using Microsoft.Xna.Framework;
using Legend.Object;
using Microsoft.Xna.Framework.Graphics;

namespace Legend.Component.Prop
{
    public class BoxDamaging : IDamaging
    {
        public enum BoxStatus
        {
            Empty,
            Unknown,
            Revealed,
        }

        private BoxStatus status = BoxStatus.Unknown;

        public BoxDamaging()
        {
            DamageDisposition = DamageType.OnlyWithWeapon | DamageType.Box;
        }

        public override string Status
        {
            get { return status.ToString(); }
        }

        public override ResolveResult ResolveDamaging(IDamaging.DamageType fromDisposition, float fromDamage, string fromStatus)
        {
            if (status != BoxStatus.Unknown && (fromDisposition & DamageType.Hero) != 0)
            {
                return ResolveResult.Disappear; // status = BoxStatus.Empty;
            }
            else if ((fromDisposition & DamageType.Weapon) != 0)
            {
                IRenderable renderable = Owner.FindComponent<IRenderable>();
                if (renderable == null)
                {
                    SimpleRenderable render = Owner.CreateComponent<SimpleRenderable>();
                    render.Model = GameObjectManager.Contents.Load<Model>("box01");
                    status = BoxStatus.Unknown;

                    Collision collision = Owner.CreateComponent<Collision>();
                    collision.LocalBoundingBox = Legend.Helper.Mesh.GetModelBoundingBox(render.Model);
                    collision.CollisionType = ICollision.CollisionTypes.Block;
                }
                else if (status == BoxStatus.Unknown)
                {
                    Owner.RemoveComponent<IRenderable>();

                    SimpleRenderable render = Owner.CreateComponent<SimpleRenderable>();
                    render.Model = GameObjectManager.Contents.Load<Model>("sight");
                    render.AlphaBlend = true;
                    render.CastShadow = true;

                    IMovement movement = Owner.FindComponent<IMovement>();
                    movement.Rotate(Vector3.Right, (float)Math.PI / 4);
                    status = BoxStatus.Revealed;

                    ICollision collision = Owner.FindComponent<ICollision>();
                    collision.CollisionType = ICollision.CollisionTypes.Through;
                }
            }

            return ResolveResult.Continue;
        }
    }
}
